# -*- coding: utf-8 -*-
# @Time : 2022/3/28 11:59

from src.ApiTest.ApiInfo.Database.api_database import ApiInfo, ApiInfoSchema
from src.User.Database.user_database import User, UserSchema
from Common.yaml_method import YamlMethod
from sqlalchemy import and_
import ast


class ApiList:
    """
    获取接口列表接口
    """

    @staticmethod
    def api_list(page, limit, api_name, module_name, user_id, project_id):
        """
        获取接口列表接口
        :param page: 页码
        :param limit: 每页多少条数据
        :param api_name: 接口名称
        :param module_name: 模块名称
        :param user_id: user_id
        :param project_id: 项目ID
        :return:
        """

        code = YamlMethod().read_data('code.yaml')['code']

        if project_id != '':
            if api_name != '':
                if module_name != '':
                    data = ApiInfo.query.filter_by(projectId=project_id, apiName=api_name, moduleName=module_name)\
                        .paginate(page=page, per_page=limit)
                else:
                    data = ApiInfo.query.filter_by(projectId=project_id, apiName=api_name).\
                        paginate(page=page, per_page=limit)
            else:
                if module_name != '':
                    data = ApiInfo.query.filter_by(projectId=project_id, moduleName=module_name)\
                        .paginate(page=page, per_page=limit)
                else:
                    data = ApiInfo.query.filter_by(projectId=project_id).paginate(page=page, per_page=limit)
        else:
            # 先查询用户所属项目信息
            user = User.query.filter_by(id=user_id).first()
            user_schema = UserSchema()
            user_data = user_schema.dump(user)
            user_project_id = user_data['user_projects']
            id_list = ast.literal_eval(user_project_id)
            # 开始构建过滤条件
            filters = [ApiInfo.projectId.in_(id_list)]
            if api_name != '':
                filters.append(ApiInfo.apiName == api_name)
                if module_name != '':
                    filters.append(ApiInfo.moduleName == module_name)
                    data = ApiInfo.query.filter(and_(*filters)).paginate(page=page, per_page=limit)
                else:
                    data = ApiInfo.query.filter(and_(*filters)).paginate(page=page, per_page=limit)
            else:
                if module_name != '':
                    filters.append(ApiInfo.moduleName == module_name)
                    data = ApiInfo.query.filter(and_(*filters)).paginate(page=page, per_page=limit)
                else:
                    data = ApiInfo.query.filter(ApiInfo.projectId.in_(id_list)).paginate(page=page, per_page=limit)

        info = []
        if data:
            for i in data.items:
                api_schema = ApiInfoSchema()
                api_data = api_schema.dump(i)
                # 将单条信息添加到info中
                info.append(api_data)

        total = data.total
        has_more = data.has_next
        res = {
            'code': code[0],
            'message': 'success',
            'data': {
                'apiList': info,
                'total': total,
                'has_more': has_more
            }
        }

        return res
